package com.yt.app.provider.service.impl;

import com.yt.app.core.interfaces.UserReservationService;
import com.yt.app.core.model.UserOrderDetail;
import com.yt.app.core.model.UserReservation;
import com.yt.app.provider.dao.UserOrderDetailDao;
import com.yt.app.provider.dao.UserReservationDao;
import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.core.service.GenericServiceImpl;

import java.util.List;

public class UserReservationServiceImpl extends GenericServiceImpl<UserReservation, Long> implements UserReservationService {
	private UserReservationDao dao;

	private UserOrderDetailDao userOrderDetailDao;

	public UserReservationServiceImpl() {
	}

	public UserReservationServiceImpl(UserReservationDao dao) {
		super(dao);
		this.dao = dao;
	}

	public void saveReservaction(UserReservation userReservation){
		if(userReservation.getId() != null){
			this.dao.update(userReservation);
		}else {
			this.dao.insert(userReservation);
			if (userReservation.getUserOrderDetailId() != null) {
				userOrderDetailDao.decreaseLeftCount(userReservation.getUserOrderDetailId());
			}
		}
	}

	@Override
	public UserReservation getByOrderNo(String orderNo) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_orderNo_S_EQ", orderNo);
		filter.setSortField("id", true);
		filter.setPagingBean(new PagingBean(0, 1));
		List<UserReservation> list = this.dao.find(filter);
		if(list != null && !list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	public UserOrderDetailDao getUserOrderDetailDao() {
		return userOrderDetailDao;
	}

	public void setUserOrderDetailDao(UserOrderDetailDao userOrderDetailDao) {
		this.userOrderDetailDao = userOrderDetailDao;
	}
}
